package com.wenli.graduate2022.wenliGreenShop.repository.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenli.graduate2022.wenliGreenShop.entity.Address;
import com.wenli.graduate2022.wenliGreenShop.entity.Admin;
import com.wenli.graduate2022.wenliGreenShop.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HuangHai
 * @date 2021/9/26 20:32
 */
@Repository   //交给spring容器管理
//@Mapper   //告诉spring这是一个mapper
public interface UserMapper {
    @Results(id = "user",value = {  //写在findByPassword方法上会默认使用该封装规则，其他方法要明确调用该方法才能使用该规则
            @Result(property = "userAddress",column = "user_address",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.AddressMapper.findById",
                            fetchType = FetchType.LAZY)), //所在地地址要详细查
            @Result(property = "userHomeplace",column = "user_homeplace",
                    one = @One(select ="com.wenli.graduate2022.wenliGreenShop.repository.mapper.AddressMapper.findById",
                    fetchType = FetchType.LAZY)) //家乡会粗略查
    })
    @Select("select * from user where user_name=#{name} and user_password=#{password}")
    User findByPassword(String name,String password);


    @Select("select * from user")
    @ResultMap("user")
    List<User> findAll();

    @Select("select * from user")
    @ResultMap("user")
    IPage<User> findByPage(Page page);

    @Select("select * from user where user_id = #{id}")
    @ResultMap("user")
    User findById(Integer id);

    /**
     * 模糊查询
     * @param name
     * @return
     */
    @Select("select * from user where user_name like concat( '%',#{name},'%')")
    @ResultMap("user")
    IPage<User> findByName(Page page,String name);

    @Options(useGeneratedKeys = true, keyProperty = "userId",keyColumn = "user_id")
    @Insert("insert into user(user_name,user_nickname,user_password,user_realname,user_gender,user_birthday" +
            "user_address,user_homeplace,user_profile_picture_src) values(#{userName},#{userNickname},#{userPassword}," +
            "#{userRealname},#{userGender},#{userBirthday},#{userAddress.addressAreaId},#{userHomeplace.addressAreaId},#{userProfilePictureSrc})")
    @ResultMap("user")
    int insert(User user);

    @Update("update user set user_name=#{userName},user_nickname=#{userNickname},user_password=#{userPassword}," +
            "user_realname=#{userRealname},user_gender=#{userGender},user_birthday=#{userBirthday}," +
            "user_address=#{userAddress.addressAreaId},user_homeplace=#{userHomeplace.addressAreaId},user_profile_picture_src=#{userProfilePictureSrc}" +
            "where user_id = #{userId}")
    @ResultMap("user")
    int update(User user);

    @Update("update user set user_name=#{userName},user_nickname=#{userNickname},user_password=#{userPassword}," +
            "user_realname=#{userRealname},user_gender=#{userGender},user_birthday=#{userBirthday}," +
            "user_address=#{userAddress.addressAreaId},user_homeplace=#{userHomeplace.addressAreaId},user_profile_picture_src=#{userProfilePictureSrc} where user_id=#{id} and user_password = #{password} ")
    int updatePassword(User user, Integer id, String password);


    @Update("update user set user_password = 123456 where user_id = #{id}")
    @ResultMap("user")
    int initPassword(Integer id);  //初始化用户密码为123456


    @Delete("delete from user where user_id = #{id}")
    @ResultMap("user")
    int delete(Integer id);

}
